import { CommunityPost } from '../types/community';
import { SECTIONS, TOPICS, TAGS } from '../constant/communityData';

// 模拟API延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

export const fetchCommunityPosts = async (sectionId: number, page: number): Promise<{
  posts: CommunityPost[];
  total: number;
  hasMore: boolean;
}> => {
  // 模拟API请求延迟
  await delay(800);

  const sectionName = SECTIONS[sectionId - 1];
  const sectionTopics = TOPICS[sectionName as keyof typeof TOPICS];
  
  const posts = Array(10).fill(null).map((_, index) => {
    const postIndex = (page - 1) * 10 + index;
    const topicIndex = postIndex % sectionTopics.length;
    
    return {
      id: page * 1000 + index,
      title: sectionTopics[topicIndex],
      content: `这是一篇关于${sectionTopics[topicIndex]}的深度讨论，欢迎大家参与交流...`,
      author: {
        id: index + 1,
        name: `书友${Math.floor(Math.random() * 1000)}`,
        avatar: `https://picsum.photos/50/50?random=${page * 10 + index}`
      },
      coverImage: `https://picsum.photos/800/400?random=${sectionId * 100 + page * 10 + index}`,
      likes: Math.floor(Math.random() * 1000),
      comments: Math.floor(Math.random() * 100),
      createTime: new Date(Date.now() - Math.random() * 10000000000).toISOString(),
      tags: Array(Math.floor(Math.random() * 2) + 1)
        .fill(null)
        .map(() => TAGS[Math.floor(Math.random() * TAGS.length)])
    };
  });

  return {
    posts,
    total: 50, // 每个板块总共50条数据
    hasMore: page * 10 < 50 // 是否还有更多数据
  };
};

export const fetchPostDetail = async (sectionId: number, postId: number): Promise<CommunityPost> => {
  await delay(800); // 模拟网络延迟

  const sectionName = SECTIONS[sectionId - 1];
  const sectionTopics = TOPICS[sectionName as keyof typeof TOPICS];
  const topicIndex = postId % sectionTopics.length;

  return {
    id: postId,
    title: sectionTopics[topicIndex],
    content: `这是一篇关于${sectionTopics[topicIndex]}的深度讨论。

这里是详细内容，包含了更多的讨论内容和见解...

1. 首先，我们来谈谈这个主题的背景
2. 接着，让我们深入探讨核心问题
3. 最后，分享一些个人的思考和建议

希望这篇文章能够引发大家的思考和讨论，欢迎在评论区留下你的见解。`,
    author: {
      id: 1,
      name: `书友${Math.floor(Math.random() * 1000)}`,
      avatar: `https://picsum.photos/50/50?random=${postId}`
    },
    coverImage: `https://picsum.photos/800/400?random=${sectionId * 100 + postId}`,
    likes: Math.floor(Math.random() * 1000),
    comments: Math.floor(Math.random() * 100),
    createTime: new Date(Date.now() - Math.random() * 10000000000).toISOString(),
    tags: Array(Math.floor(Math.random() * 2) + 1)
      .fill(null)
      .map(() => TAGS[Math.floor(Math.random() * TAGS.length)])
  };
}; 